package com.platform.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.NotBlank;

import org.activiti.engine.repository.Model;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.common.validator.ValidatorUtils;
//import com.platform.modules.act.entity.ActReModelEntity;
import com.platform.modules.sys.entity.SysConfigEntity;
import com.platform.modules.sys.service.SysConfigService;

/**
 * 系统配置信息
 *
 * @author 肖亮
 */
@Api(tags = "SysConfigController|系统配置管理")
@RestController
@RequestMapping("/sys/config")
public class SysConfigController extends AbstractController {
	@Autowired
	private SysConfigService sysConfigService;

	/**
	   * 所有系统配置列表
	 *
	 * @param params
	   *     查询参数
	 * @return RestResponse
	 */
	@ApiOperation(value = "查询所有系统配置列表", notes = "根据type获取系统配置")
	  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "type", value = "系统配置type值", dataType = "String")
    })
	@RequiresPermissions("sys:config:list")
	@PostMapping("/list")
	public RestResponse list(String type) {
		 if(type==null||type.equals("")) {
			 type="1";
		 }
		   Map<String,Object> params=new HashMap<String,Object>();
			if (Constant.STR_ONE.equals(type)) {
				// 查询所有系统配置列
				params.put("pattern", "*");
			} else if (Constant.STR_TWO.equals(type)) {
				// 查询session缓存
				params.put("pattern", Constant.SESSION + "*");
			} else if (Constant.STR_THREE.equals(type)) {
				// 查询系统缓存
				params.put("pattern", Constant.SYS_CACHE + "*");
			} else if (Constant.STR_FOUR.equals(type)) {
				// 查询业务缓存
				params.put("pattern", Constant.MTM_CACHE + "*");
			}
			params.put("type", type);
		Page page = sysConfigService.queryPage(params);
		return RestResponse.success().put("page", page);
	}

	/**
	 * 根据主键查询详情
	 *
	 * @param id
	 *            主键
	 * @return RestResponse
	 */
	@ApiOperation(value = "查询详情", notes = "根据主键查询详情")
	  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "id", value = "主键", required = true, dataType = "String")
    })
	@RequiresPermissions("sys:config:info")
	@PostMapping("/info/{id}")
	public RestResponse info(String id) {
		SysConfigEntity config = sysConfigService.getById(id);

		return RestResponse.success().put("config", config);
	}

	/**
	 * 保存系统配置
	 *
	 * @param config
	 *            config
	 * @return RestResponse
	 */
	@ApiOperation(value = "保存系统配置", notes = "保存系统配置")
	@SysLog("保存系统配置")
	@PostMapping("/save")
	@RequiresPermissions("sys:config:save")
	public RestResponse save(SysConfigEntity config) {
	   
		ValidatorUtils.validateEntity(config);

		sysConfigService.add(config);
		return RestResponse.success();
	}
	
	
	/**
	 * 修改系统配置
	 *
	 * @param config
	 *            config
	 * @return RestResponse
	 */
	@ApiOperation(value = "修改系统配置", notes = "修改系统配置")
	@SysLog("修改系统配置")
	@PostMapping("/update")
	@RequiresPermissions("sys:config:update")
	public RestResponse update(@RequestBody SysConfigEntity config) {
		ValidatorUtils.validateEntity(config);

		sysConfigService.update(config);

		return RestResponse.success();
	}

	/**
	 * 删除系统配置
	 *
	 * @param ids
	 *            ids
	 * @return RestResponse
	 */
	@ApiOperation(value = "删除系统配置", notes = "根据id删除模型（支持多id删除）")
	@SysLog("删除系统配置")
	@PostMapping("/delete")
	@RequiresPermissions("sys:config:delete")
	public RestResponse delete(String[] ids) {
		sysConfigService.deleteBatch(ids);

		return RestResponse.success();
	}

}
